<template>
  <div class="my-profile-bg">
    <el-card class="my-profile-card">
      <div class="profile-header">
        <div class="profile-nav">
          <el-button size="small" @click="goUserInfo">完善信息</el-button>
          <el-button size="small" @click="goidhistory">认证历史记录</el-button>
        </div>
        <el-button class="back-home-btn" size="small" type="primary" @click="goHome">返回首页</el-button>
      </div>
      <div class="my-profile-title">我的信息</div>
      <el-divider />
      <div v-if="user">
        <div class="profile-avatar-wrap">
          <img :src="user && user.avatar ? user.avatar : require('@/assets/default-avatar.png')" class="profile-avatar" />
        </div>
        <el-descriptions :column="1" border>
          <el-descriptions-item label="用户ID">{{ user ? user.id : '--' }}</el-descriptions-item>
          <el-descriptions-item label="手机号">{{ user ? user.phone : '--' }}</el-descriptions-item>
          <el-descriptions-item label="真实姓名">{{ user ? user.real_name : '--' }}</el-descriptions-item>
          <el-descriptions-item label="身份">{{ user ? (user.type === 1 ? '车主' : '乘客') : '--' }}</el-descriptions-item>
          <el-descriptions-item label="实名状态">{{ user ? (user.status === 0 ? '未认证' : user.status === 1 ? '已认证' : user.status === 2 ? '禁用' : '--') : '--' }}</el-descriptions-item>
        </el-descriptions>
        <el-button type="danger" size="large" class="logout-btn" @click="logout">退出登录</el-button>
      </div>
      <div v-else>
        <el-empty description="请先登录" />
        <el-button type="primary" @click="goLogin">去登录</el-button>
      </div>
      <el-dialog v-model="showAuthDialog" title="认证历史记录" width="500px" :close-on-click-modal="false">
        <el-table v-if="!loadingAuth" :data="authHistory" style="width: 100%">
          <el-table-column prop="auth_time" label="认证时间" width="180">
            <template #default="scope">
              <span>{{ formatDate(scope.row.auth_time) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="auth_type" label="类型" width="80">
            <template #default="scope">
              <span>{{ scope.row.auth_type === 0 ? '人脸识别' : '其他' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="auth_status" label="状态" width="80">
            <template #default="scope">
              <span v-if="scope.row.auth_status === 1" style="color:green">通过</span>
              <span v-else style="color:red">未通过</span>
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注" />
        </el-table>
        <div v-else style="text-align:center;padding:30px 0;">加载中...</div>
        <template #footer>
          <el-button @click="showAuthDialog = false">关闭</el-button>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { http } from '../utils/request'
import { ElMessage } from 'element-plus'

const router = useRouter()
const user = ref(null)
const showAuthDialog = ref(false)
const authHistory = ref([])
const loadingAuth = ref(false)

function formatDate(val) {
  if (!val) return '--'
  const d = new Date(val)
  const pad = n => n.toString().padStart(2, '0')
  return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
}

function fetchUser() {
  const token = localStorage.getItem('token')
  const userid = localStorage.getItem('userid')
  if (!token || !userid) {
    user.value = null
    return
  }
  http.get(`/login/users/${userid}/`).then(res => {
    if (res.data && res.data.id) {
      user.value = res.data
    } else {
      user.value = null
    }
  }).catch(() => {
    user.value = null
  })
}

async function goidhistory() {
  showAuthDialog.value = true
  loadingAuth.value = true
  const userid = localStorage.getItem('userid')
  http.get(`/login/face/?userid=${userid}`).then(res => {
    authHistory.value = res.data.data || []
    loadingAuth.value = false
  })
}

function logout() {
  localStorage.removeItem('token')
  localStorage.removeItem('userid')
  ElMessage.success('已退出登录')
  router.replace('/login')
}

function goLogin() {
  router.replace('/login')
}
function goHome() {
  router.push('/')
}
function goUserInfo() {
  router.push('/user-info')
}

onMounted(() => {
  fetchUser()
})
</script>

<style scoped>
.my-profile-bg {
  min-height: 100vh;
  background: linear-gradient(135deg, #fff 60%, #ffe5cc 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}
.my-profile-card {
  width: 480px;
  border-radius: 18px;
  box-shadow: 0 4px 24px 0 rgba(34,34,34,0.08);
  padding: 32px 36px 24px 36px;
  position: relative;
}
.profile-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.profile-nav {
  display: flex;
  gap: 8px;
}
.back-home-btn {
  margin-left: auto;
}
.my-profile-title {
  font-size: 26px;
  font-weight: bold;
  color: #222;
  text-align: center;
  margin-bottom: 8px;
  letter-spacing: 2px;
}
.profile-avatar-wrap {
  display: flex;
  justify-content: center;
  margin-bottom: 18px;
}
.profile-avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  border: 2px solid #ff8200;
  object-fit: cover;
}
.logout-btn {
  width: 100%;
  margin-top: 18px;
  font-size: 18px;
  font-weight: bold;
}
</style> 